<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_fctrl_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/forms/_menu/menu_item/menu_item_form_ctrl.class.php');


class Menu_Item_Edit_WP extends TCMS_Admin_Page_W_FCtrl_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_menu/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('menu');
	}


	protected function create_form_ctrl() {
		$dbc = $this->get_dbc();
		$tvm = $this->get_tvm();

		$ret = new Menu_Item_Form_Ctrl('menu_item', $tvm, $dbc, 'obj_id');
		$menu_id = $this->get_transit_var('menu');
		$ret->set_permanent_external_param('menu', $menu_id);

		$this->export('menu_id', $menu_id);

		return $ret;
	}


	protected function on_add_new() {
		$context = $this->get__context();
		if (!$context->exists_in_get('type')) {
			throw new Tangra_Module_Exception('Missing "type" parameter');
		}

		if (!$context->exists_in_get('ret')) {
			throw new Tangra_Module_Exception('Missing "ret" parameter');
		}

		if (!$context->exists_in_get('parent')) {
			throw new Tangra_Module_Exception('Missing "parent" parameter');
		}

		$this->form_ctrl->set_permanent_external_param('type', $context->get_from_get('type'));
		$this->form_ctrl->set_permanent_external_param('ret', $context->get_from_get('ret'));
		$this->export('ret', $context->get_from_get('ret'));
		$this->form_ctrl->set_permanent_external_param('parent', $context->get_from_get('parent'));

		$this->form_ctrl->set_permanent_external_param('add_new', 1);
		$this->export('add_new', 1);

		$this->form_ctrl->prepare_order($context->get_from_get('parent'), $this->get_transit_var('menu'));

		$this->form_ctrl->set_permanent_external_param('user_id', $this->get_user()->get_id());
	}


	protected function on_received_id($object_id) {
		$context = $this->get__context();

		if (!$context->exists_in_get('ret')) {
			throw new Tangra_Module_Exception('Missing "ret" parameter');
		}

		$this->form_ctrl->set_permanent_external_param('ret', $context->get_from_get('ret'));
		$this->export('ret', $context->get_from_get('ret'));

		$this->export('menu_item_id', $object_id);

		$this->form_ctrl->set_permanent_external_param('user_id', $this->get_user()->get_id());
	}


	protected function create_default_redir_view() {
		$object = $this->form_ctrl->get_object();
		$redir_view = $this->create_redirect_view($this->get_redir_on_success(), array('manage' => $this->get_transit_var('menu')));

		return $redir_view;
	}
}